package lz.controls.listClasses
{
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.events.IOErrorEvent;
	import lz.controls.containers.UILoader;

	/**
	 * ImageCell 是 TileList 组件的默认单元格渲染器。
	 * ImageCell 类接受 label 和 source 属性，并显示缩略图和单行标签。
	 * 注意：正在从不同的域或沙箱加载内容时，内容的属性可能会由于安全原因而无法访问。
	 * 有关域安全如何影响加载过程的详细信息，请参阅“ProLoader 类”。
	 */
	public class ImageCell extends CellRenderer implements ICellRenderer
	{

		/**
		 * @private (protected)
		 */
		protected var textOverlay:Shape;

		/**
		 * @private (protected)
		 */
		protected var loader:UILoader;
		/**
		 * @private
		 */
		private static var defaultStyles:Object={imagePadding: 1, textOverlayAlpha: 0.7};

		/**
		 */
		public static function getStyleDefinition():Object
		{
			return mergeStyles(defaultStyles, CellRenderer.getStyleDefinition());
		}

		/**
		 * Creates a new ImageCell instance.
		 */
		public function ImageCell()
		{
			super();

			loader=new UILoader();

			loader.addEventListener(IOErrorEvent.IO_ERROR, handleErrorEvent, false, 0, true);

			loader.autoLoad=true;
			loader.scaleContent=true;
			addChild(loader);
		}

		/**
		 * Gets or sets the list properties that are applied to the cell, for example,
		 * the <code>index</code> and <code>selected</code> values. These list properties
		 * are automatically updated after the cell is invalidated.
		 *
		 * <p>Although the listData property returns an instance of ListData, in the
		 * TileList cells receive an instance of <code>TileListData</code> instead,
		 * which contains a <code>source</code> property.</p>
		 */
		override public function get listData():ListData
		{
			return _listData;
		}

		/**
		 * @private (setter)
		 */
		override public function set listData(value:ListData):void
		{
			_listData=value;
			label=_listData.label;
			var newSource:Object=(_listData as TileListData).source;
			if (source != newSource)
			{ // Prevent always reloading...
				source=newSource;
			}
		}


		/**
		 * Gets or sets an absolute or relative URL that identifies the
		 * location of the SWF or image file to load, the class name
		 * of a movie clip in the library, or a reference to a display
		 * object.
		 */
		public function get source():Object
		{
			return loader.source;
		}

		/**
		 * @private (setter)
		 */
		public function set source(value:Object):void
		{
			loader.source=value;
		}

		/**
		 * @private (protected)
		 */
		override protected function configUI():void
		{
			super.configUI();

			textOverlay=new Shape();
			var g:Graphics=textOverlay.graphics;
			g.beginFill(0xFFFFFF);
			g.drawRect(0, 0, 100, 100);
			g.endFill();
		}

		/**
		 * @private (protected)
		 */
		override protected function draw():void
		{
			super.draw();
		}

		/**
		 * @private (protected)
		 */
		override protected function drawLayout():void
		{
			var imagePadding:Number=getStyleValue("imagePadding") as Number;
			loader.move(imagePadding, imagePadding);

			var w:Number=width - (imagePadding * 2);
			var h:Number=height - imagePadding * 2;
			if (loader.width != w && loader.height != h)
			{
				loader.setSize(w, h);
			}
			loader.drawNow(); // Force validation!

			// Position textfield
			if (_label == "" || _label == null)
			{
				if (contains(textField))
				{
					removeChild(textField);
				}
				if (contains(textOverlay))
				{
					removeChild(textOverlay);
				}
			}
			else
			{
				var textPadding:Number=getStyleValue("textPadding") as Number;
				textField.width=Math.min(width - textPadding * 2, textField.textWidth + 5);
				textField.height=textField.textHeight + 5;
				textField.x=Math.max(textPadding, width / 2 - textField.width / 2);
				textField.y=height - textField.height - textPadding; // Multiline is not supported.

				textOverlay.x=imagePadding;
				textOverlay.height=textField.height + textPadding * 2;
				textOverlay.y=height - textOverlay.height - imagePadding;
				textOverlay.width=width - imagePadding * 2;
				textOverlay.alpha=getStyleValue("textOverlayAlpha") as Number;

				addChild(textOverlay);
				addChild(textField);
			}

			background.width=width;
			background.height=height;
		}

		/**
		 * @private (protected)
		 */
		protected function handleErrorEvent(event:IOErrorEvent):void
		{
			dispatchEvent(event);
		}

	}
}
